package MAIN;

import java.util.TimerTask;


import config.AliDnsConfig;
import ip_query.ip;
import util.AliDomainUtil;

public class ddns_factory  extends TimerTask {
	boolean isExecute = true;
	long nextExecutTime;
	ip IP=new ip();
    AliDomainUtil util = new AliDomainUtil();

	public void run() {
		if(!isExecute){
			if(nextExecutTime <= System.currentTimeMillis()){
				isExecute = true;
			}else {
				return;
			}
		}

		String recordIp  = util.getRecordIp();

        if(recordIp ==null) {

			String ip= IP.getIP();
            String recordId = util.addDns(ip);
            if(recordId == null){
                System.out.println("已添加解析，不可重复添加，请稍等");
            }else{
                System.out.println("初次运行域名绑定为"+ ip);
				isExecute = false;
				nextExecutTime = System.currentTimeMillis()+ AliDnsConfig.stopExecuteTime;
            }
		}else {
			
			String ip=IP.getIP();
			if(ip.equals(recordIp)) {
				System.out.println("Ip没有变化");
			}else {
				Boolean result = util.updateDns(ip);
				if(result){
					System.out.println("IP改变进行了新的绑定，新的IP为"+ ip);
					isExecute = false;
					nextExecutTime = System.currentTimeMillis()+AliDnsConfig.stopExecuteTime;
				}
			}
			
		}
		
		
		
	}

}
